package gui;

import javax.swing.JPanel;

import application.Author;
import application.Story;

import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JSeparator;

public class AppPanelAuthor extends JPanel {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 3532086094480804100L;
	private final JTextField txtName = new JTextField();
	private final JTextField txtFirstname = new JTextField();
	private final JTextField txtEmail = new JTextField();
	private final JTextPane txtAddress = new JTextPane();
	private final JLabel lblName = new JLabel("Name");
	private final JLabel lblFirstname = new JLabel("Firstname");
	private final JLabel lblEmail = new JLabel("Email");
	private final JLabel lblAddress = new JLabel("Address");
	private final JTextField txtWeb = new JTextField();
	private final JLabel lblWebsite = new JLabel("Website");
	private final JTextField txtAlias = new JTextField();
	private final JLabel lblAlias = new JLabel("Alias");
	private final JTextField txtfbuser = new JTextField();
	private final JTextField txtfbpass = new JTextField();
	private final JTextField txttwuser = new JTextField();
	private final JTextField txttwpass = new JTextField();
	private final JLabel lblFacebook = new JLabel("Facebook");
	private final JLabel lblTwitter = new JLabel("Twitter");
	private final JScrollPane scrollPane = new JScrollPane();
	private final JLabel lblUsername = new JLabel("username");
	private final JLabel lblPassword = new JLabel("password");
	private final JButton btnSave = new JButton("Save");
	private final JLabel lblAuthorSettings = new JLabel("Author Settings");
	private final JSeparator separator = new JSeparator();
	private final JSeparator separator_1 = new JSeparator();
	
	
	
	/**
	 * Create the panel.
	 */
	public AppPanelAuthor(final String dbFile, Story s) {
		
		final Author tmp = s.getStoryAuthor();
		final Story story = s;
		
		txttwpass.setColumns(10);
		txttwuser.setColumns(10);
		txtfbpass.setColumns(10);
		txtfbuser.setColumns(10);
		txtWeb.setColumns(10);
		setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(75px;default)"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(125px;default)"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(125px;default)"),
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("max(125px;default)"),
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),}));
		
		add(lblAuthorSettings, "4, 2, 5, 1");
		
		add(separator, "4, 4, 5, 1");
		add(lblName, "4, 6, left, default");
		txtName.setColumns(10);
		add(txtName, "6, 6, 3, 1, fill, default");
		add(lblFirstname, "4, 8, left, default");
		txtFirstname.setColumns(10);
		add(txtFirstname, "6, 8, 3, 1, fill, default");
		add(lblAlias, "4, 10, left, default");
		txtAlias.setColumns(10);
		add(txtAlias, "6, 10, 3, 1, fill, default");
		add(lblEmail, "4, 12, left, default");
		txtEmail.setColumns(10);
		add(txtEmail, "6, 12, 3, 1, fill, default");
		add(lblAddress, "4, 14, left, top");
		add(scrollPane, "6, 14, 3, 1, fill, fill");
		scrollPane.setViewportView(txtAddress);
		add(lblWebsite, "4, 16, left, default");
		add(txtWeb, "6, 16, 3, 1, fill, default");
		add(lblUsername, "6, 18");
		add(lblPassword, "8, 18");
		add(lblFacebook, "4, 20, left, default");
		add(txtfbuser, "6, 20, fill, default");
		add(txtfbpass, "8, 20, fill, default");
		add(lblTwitter, "4, 22, left, default");
		add(txttwuser, "6, 22, fill, default");
		add(txttwpass, "8, 22, fill, default");
		
		txtAddress.setText(tmp.getAddress());
		txtAlias.setText(tmp.getAlias());
		txtEmail.setText(tmp.getEmail());
		txtfbpass.setText(tmp.getFbpass());
		txtfbuser.setText(tmp.getFbuser());
		txtFirstname.setText(tmp.getFirstName());
		txtName.setText(tmp.getName());
		txttwpass.setText(tmp.getTwpass());
		txttwuser.setText(tmp.getTwuser());
		txtWeb.setText(tmp.getWeb());
		
		add(separator_1, "4, 24, 5, 1");
		
		
		add(btnSave, "4, 26, default, top");
		
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				updateAuthor(dbFile, story);
			}
		});
		
		this.setVisible(true);

	}

	protected void updateAuthor(String dbFile, Story s) {
		Author a = new Author();
		a.setAddress(txtAddress.getText());
		a.setAlias(txtAlias.getText());
		a.setEmail(txtEmail.getText());
		a.setFbpass(txtfbpass.getText());
		a.setFbuser(txtfbuser.getText());
		a.setFirstName(txtFirstname.getText());
		a.setName(txtName.getText());
		a.setTwpass(txttwpass.getText());
		a.setTwuser(txttwuser.getText());
		a.setWeb(txtWeb.getText());
		s.dbEditAuthor(dbFile, a);
		StoryLineAppGUI.lblStatusOutput.setText("Author settings updated");
	}

}
